﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Interaction with other view models</title>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Interaction with other view models</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Now that we&apos;ve seen how easy it is to communicate between the View Model and the Model, you want more, right? I know how it is: “You let &apos;em have one finger, they take your whole hand”. No worries, you can have my right hand, as long as I can keep my left one. Anyway, the developers of Catel are prepared for this. So, let&apos;s talk about the interaction with other View Models.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Say, you have a multiple document interface (MDI as it was called in the old days). If you are following MVVM principles, every document (or tab) has its own View Model. Then, you want to be aware of updates of a single type of View Model. Say, for example, that there is a View Model representing a family called </text><text styleclass="Normal" style="font-style:italic;" translate="true">FamilyViewModel</text><text styleclass="Normal" translate="true">. This View Model is probably interested in changes in the </text><text styleclass="Normal" style="font-style:italic;" translate="true">PersonViewModel</text><text styleclass="Normal" translate="true">.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">View Model Manager</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Let&apos;s start with the basics. As we have learned earlier in this article, all View Models created with the help of Catel derive from the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ViewModelBase" styleclass="Normal" translate="true">ViewModelBase</link><text styleclass="Normal" translate="true"> class. One of the things that this class does is that it registers itself with the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ViewModelManager" styleclass="Normal" translate="true">ViewModelManager</link><text styleclass="Normal" translate="true"> class when it is being created, and it unregisters itself again when it is closed. So, simply said, </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ViewModelManager" styleclass="Normal" translate="true">ViewModelManager</link><text styleclass="Normal" translate="true"> is a class that holds a reference to all existing View Models at the moment.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">InterestedInAttribute</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Now that we know about the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ViewModelManager" styleclass="Normal" translate="true">ViewModelManager</link><text styleclass="Normal" translate="true"> class, and know that there is a repository that holds all of the live instances of all View Model classes, it should be fairly easy to communicate with other View Models. It actually is; you just have to decorate a View Model with </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_InterestedInAttribute" styleclass="Normal" translate="true">InterestedInAttribute</link><text styleclass="Normal" translate="true">, as shown below:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">[InterestedIn(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">typeof</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">(FamilyViewModel))]</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">class</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PersonViewModel</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">:</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ViewModelBase</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">A View Model can have multiple </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_InterestedInAttribute" styleclass="Normal" translate="true">InterestedInAttribute</link><text styleclass="Normal" translate="true"> instances, so it is possible to subscribe to multiple View Model types at the same time. Once a View Model is decorated with </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_InterestedInAttribute" styleclass="Normal" translate="true">InterestedInAttribute</link><text styleclass="Normal" translate="true">, the View Model will receive all changes (and of course, the View Model that caused the change) via the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_MVVM_ViewModelBase_OnViewModelPropertyChanged_2_7c54ef9a" styleclass="Normal" translate="true">OnViewModelPropertyChanged</link><text styleclass="Normal" translate="true"> method, as shown below:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#000000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#000000;" translate="true">/// Called when a property has changed for a view model</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#000000;" translate="true">/// type that the current view model is interested in. This can</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#000000;" translate="true">/// be accomplished by decorating the view model</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#000000;" translate="true">/// with the &lt;see cref=&quot;InterestedInAttribute&quot;/&gt;.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#000000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#000000;" translate="true">/// &lt;param name=&quot;viewModel&quot;&gt;The view model.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#000000;" translate="true">/// &lt;param name=&quot;propertyName&quot;&gt;Name of the property.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">protected</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">override</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">OnViewModelPropertyChanged(IViewModel</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">viewModel,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">propertyName)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#000000;" translate="true">// You can now do something with the changed property</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">Note that it is also possible to get notified of commands that are being executed on other view models</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">MessageMediator</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Catel also offers a solution to the message mediator pattern in the form of the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="Messaging_Message_mediator" styleclass="Normal" translate="true">MessageMediator</link><text styleclass="Normal" translate="true"> class. This is all described in the next section &quot;</text><link displaytype="text" defaultstyle="true" type="topiclink" href="GS_MVVM_Message_mediator" styleclass="Normal" translate="true">Message mediator</link><text styleclass="Normal" translate="true">&quot;.</text></para>
  </body>
</topic>
